package com.tim.app.ui.activity.found;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.WindowManager;

import com.application.library.net.StringResponseCallback;
import com.application.library.widget.EmptyLayout;
import com.application.library.widget.loadmore.LoadMoreContainer;
import com.application.library.widget.loadmore.LoadMoreHandler;
import com.application.library.widget.loadmore.LoadMoreRecycleViewContainer;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.google.gson.Gson;
import com.lzy.okhttputils.cache.CacheMode;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.api.API_Discover;
import com.tim.app.server.entry.MusicAlbum;
import com.tim.app.server.result.MusicAlbumListResult;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.ui.activity.BaseActivity;
import com.tim.app.ui.adapter.found.MusciAlbumItemAdapter;
import com.tim.app.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 全部专辑
 */
public class AllMusicAlbumActivity extends BaseActivity implements LoadMoreHandler, BaseRecyclerAdapter.OnItemClickListener {

    public static final String TAG = "AllMusicAlbumActivity";

    private LoadMoreRecycleViewContainer load_more;
    private WrapRecyclerView wrvMusicAlbum;
    private EmptyLayout emptyLayout;

    private List<MusicAlbum> dataList;
    private MusciAlbumItemAdapter adapter;

    private int pageNumber = 1;

    public static void start(Context context) {
        Intent intent = new Intent(context, AllMusicAlbumActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_all_music_album;
    }


    @Override
    public void initView() {

        load_more = (LoadMoreRecycleViewContainer) findViewById(R.id.load_more);
        wrvMusicAlbum = (WrapRecyclerView) findViewById(R.id.wrvMusicAlbum);
        wrvMusicAlbum.setOverScrollMode(View.OVER_SCROLL_NEVER);

        load_more.useDefaultFooter(View.VISIBLE);
        load_more.setAutoLoadMore(true);
        load_more.setLoadMoreHandler(this);
        emptyLayout = new EmptyLayout(this, load_more);
        emptyLayout.showLoading();
        emptyLayout.setEmptyDrawable(R.drawable.ic_empty_search);
        emptyLayout.setEmptyText("没有专题", "");
        emptyLayout.setEmptyButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.showLoading();
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        wrvMusicAlbum.setLayoutManager(layoutManager);

        findViewById(R.id.ibBack).setOnClickListener(this);

        dataList = new ArrayList<>();
        adapter = new MusciAlbumItemAdapter(this, dataList);
        adapter.setOnItemClickListener(this);
        wrvMusicAlbum.setAdapter(adapter);
    }

    StringResponseCallback refreshCallback = new StringResponseCallback() {
        @Override
        public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
            if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                if (dataList != null) {
                    dataList.clear();
                }
                MusicAlbumListResult result = new Gson().fromJson(resultValue, MusicAlbumListResult.class);

                if (null != result && null != result.getRes() && null != result.getRes().getArraylist()) {
                    if (null != result.getRes() && null != result.getRes().getArraylist()) {
                        if (null != result.getRes().getArraylist().getResult()) {
                            dataList.addAll(result.getRes().getArraylist().getResult());
                            adapter.notifyDataSetChanged();
                        }
                        pageNumber = result.getRes().getArraylist().getCurrentPageNo();
                        if (pageNumber != result.getRes().getArraylist().getTotalPageCount()) {
                            load_more.loadMoreFinish(false, true);
                        } else {
                            load_more.loadMoreFinish(false, false);
                        }
                    }
                }

                if (dataList == null || dataList.size() == 0) {
                    emptyLayout.showEmpty();
                } else {
                    emptyLayout.showContent();
                }
            } else {
                ToastUtil.showToast(errMsg);
            }
            return false;
        }
    };


    StringResponseCallback moreCallback = new StringResponseCallback() {
        @Override
        public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
            if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                MusicAlbumListResult result = new Gson().fromJson(resultValue, MusicAlbumListResult.class);

                if (null != result && null != result.getRes() && null != result.getRes().getArraylist()) {
                    if (null != result.getRes() && null != result.getRes().getArraylist()) {
                        if (null != result.getRes().getArraylist().getResult()) {
                            dataList.addAll(result.getRes().getArraylist().getResult());
                            adapter.notifyDataSetChanged();
                        }
                        pageNumber = result.getRes().getArraylist().getCurrentPageNo();
                        if (pageNumber != result.getRes().getArraylist().getTotalPageCount()) {
                            load_more.loadMoreFinish(false, true);
                        } else {
                            load_more.loadMoreFinish(false, false);
                        }
                    }
                }

            } else {
                ToastUtil.showToast(errMsg);
            }
            return false;
        }
    };

    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
        API_Discover.ins().getMusicAlbumList(TAG, pageNumber + 1, AppKey.PAGE_SIZE, CacheMode.DEFAULT, moreCallback);
    }

    @Override
    public void initData() {
        API_Discover.ins().getMusicAlbumList(TAG, pageNumber, AppKey.PAGE_SIZE, CacheMode.DEFAULT, refreshCallback);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onItemClick(View view, int position, long id) {
        MusicAlbumDetailActivity.start(this, dataList.get(position), false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibBack:
                finish();
                break;
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}
